<template>
  <div class="layout">
    <div class="top">
      <div class="text">
        <div class="text-left">{{ time }}</div>
        <div v-if="mylist.nickName==null" @click="tologin">请登录</div>
        <div class="text-right">{{ mylist.nickName }}</div>
      </div>
      <div class="search">
        <span class="icon">
          <van-icon name="search" size="18" color="#0C34BA" />
        </span>
        <input type="text" placeholder="输入商品名称" v-model="m1" @click.enter="search"/>
      </div>
    </div>
    <!-- 轮播图 -->
    <div class="swiper">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="#0c43ba">
        <van-swipe-item
          v-for="(item, index) in bannerlist"
          :key="index"
          @click="goDetail(item.pid)"
        >
          <img :src="item.bannerImg" alt=""
        /></van-swipe-item>
      </van-swipe>
    </div>
    <!-- 热卖推荐 -->
    <div class="hot">
      <span class="hot-text">热卖推荐</span>
    </div>
    <!-- 热卖商品 -->
    <div class="hotshop">
      <div
        class="hotshop-item"
        v-for="(item, index) in shoplist"
        :key="index"
        @click="goDetail(item.pid)"
      >
        <HotShop :prolist="item"></HotShop>
      </div>
    </div>
  </div>
</template>
<script>
import HotShop from "../components/HotShop";
export default {
  name: "HomePage",
  components: {
    HotShop,
  },
  data() {
    return {
      shoplist: [],
      showlist: [],
      bannerlist: [],
      m1: "",
      mylist: "",
    };
  },
  computed: {
    nick() {
      return this.$store.state.nickName;
    },
    time() {
      let date = new Date();
      let hour = date.getHours();
      if (6 < hour && hour < 12) {
        return "早上好";
      } else if (12 <= hour && hour < 18) {
        return "下午好";
      } else if (18 <= hour && hour < 24) {
        return "晚上好";
      } else{
        return "深夜好"
      }
    },
  },
  created() {
    this.getType();
    this.getBanner();
    this.findMy();
  },
  methods: {
    //   查找我的数据
    findMy() {
      this.axios({
        url: "/findMy",
        method: "get",
      })
        .then((res) => {
          // 
          if (res.data.code == "A001") {
            this.mylist = res.data.result[0];
          }
        })
        .catch((err) => {
          
        });
    },
    // 获取热门推荐数据
    getType() {
      this.axios({
        method: "get",
        url: "/typeProducts",
        params: {
          key: "isHot",
          value: 1,
        },
      })
        .then((res) => {
          // 
          if (res.data.code == 500) {
            this.shoplist = res.data.result;
            this.showlist = res.data.result;
          }
        })
        .catch((err) => {
          
        });
    },
    // 搜索
    search() {
      this.axios({
        method: "get",
        url: "/search",
        params: {
          name: this.m1,
        },
      })
        .then((res) => {
          // 
          if (res.data.code == "Q001") {
            if (this.m1.length == 0) {
              this.shoplist = this.showlist;
              return;
            }
            this.shoplist = res.data.result;
          }
        })
        .catch((err) => {
          
        });
    },
    // 获取商品详情
    goDetail(pid) {
      this.$router.push({ name: "Detail", params: { pid } });
    },
    // 获取轮播数据
    getBanner() {
      this.axios({
        method: "get",
        url: "/banner",
        params: {},
      })
        .then((res) => {
          // 
          if (res.data.code == 300) {
            this.bannerlist = res.data.result;
          }
        })
        .catch((err) => {
          
        });
    },
    // 跳转到登录页面
    tologin() {
      this.$router.push({ name: "Login" });
    },
  },
};
</script>
<style lang="less" scoped>
.my-swipe .van-swipe-item {
  font-size: 20px;
  text-align: center;
  overflow: hidden;
  img {
    width: 100%;
    display: block;
  }
}
/deep/ .my-swipe {
  border-radius: 10px;
  overflow: hidden;
}
.layout {
  padding: 0 10px;
  padding-bottom: 50px;
  .top {
    background: #fff;
    font-weight: bold;
    display: flex;
    height: 50px;
    .text {
      display: flex;
      line-height: 50px;
      .text-left {
        color: #666;
        font-size: 14px;
        margin-right: 10px;
      }
      .text-right {
        font-size: 14px;
        color: #0c34ba;
      }
    }
    .search {
      background: #f7f8fa;
      margin-left: auto;
      height: 36px;
      margin-top: 9px;
      border-radius: 20px;
      .icon {
        vertical-align: middle;
        margin-left: 10px;
      }
      input {
        border: 0;
        height: 100%;
        background: #f7f8fa;
        border-radius: 20px;
      }
    }
  }

  .hot {
    height: 50px;
    background: #f2f2f2;
    line-height: 50px;
    margin: 10px 0;
    .hot-text {
      color: white;
      background: #0c34ba;
      font-size: 14px;
      padding: 10px 15px;
      border-top-right-radius: 20px;
    }
  }
  .hotshop {
    display: flex;
    flex-wrap: wrap;
    .hotshop-item {
      width: calc(~"50% - 5px");
      margin-right: 10px;
      margin-bottom: 10px;
      &:nth-of-type(2n) {
        margin-right: 0;
      }
    }
  }
}
/deep/ .van-swipe__indicator {
  width: 30px;
  height: 5px;
  border-radius: 0;
}
</style>